package com.david.rabbitmq.taskqueue;

import com.david.rabbitqm.util.ChannelUtlis;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;

/**
 * @author David
 * @version V1.0
 * @Package com.david.rabbitmq.taskqueue
 * @date 2021/7/17 9:23
 */
public class TaskQueueConsumer {
    private static final String TASK_QUEUE_NAME = "task_queue";

    public static void main(String[] args) throws IOException {
        Channel channel = ChannelUtlis.getChannel();
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String task = new String(delivery.getBody(), "UTF-8");
            System.out.println(" [x] Received '" + task + "'");
            doWork(task);
            System.out.println(" [x] Done");

        };
        channel.queueDeclare(TASK_QUEUE_NAME, false, false, false, null);
        channel.basicConsume(TASK_QUEUE_NAME, true, deliverCallback,
                consumerTag -> {});
    }
    private static void doWork(String task) {
        System.out.println("task = " + task);
    }

}
